/**
 * This tests the basic offline functionality
 * of several classes 
 */
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import junit.framework.TestCase;

public class JUnitTest extends TestCase{
	@Before
	public void setup(){
		User[] users = DatabaseUsers.users;
	}
	
	@After
	public void finish(){
		DatabaseUsers.saveUsers();
	}
	
	@Test
	public void testCalendarLoadSave() {
		/*Testing appointment's toString*/
		Appointment a = new Appointment(10, "Doc Cox", "My head hurts", 1, 2, 3, 4);
		assertEquals("Appointment toString()", 0, a.toString().compareTo(
				"Appointment\t10\tDoc Cox\tMy head hurts\t1\t2\t3\t4"));

		/*Testing appointment's fromString*/
		Appointment b = Appointment.fromString(a.toString());
		assertEquals("Appointment from String",0,a.toString().compareTo(b.toString()));
		
		Appointment c = new Appointment(100, "Doc Rox", "My foot hurts", 4, 3, 2, 1);
		Appointment d = new Appointment(1000, "Doc Roc", "My hand hurts", 5, 4, 3, 2);
		Appointment e = new Appointment(10000, "Doc Noc", "My back hurts", 5, 6, 7, 8);

		
		Calendar cal = new Calendar();
		cal.addAppointment(a);
		cal.addAppointment(c);
		cal.addAppointment(d);
		cal.addAppointment(e);
		a.approve();
		d.approve();
		cal.update();
		/*Testing Calendars to and from strings. They are large strings, so individual
		 * string tests would be unweildly.*/
		Calendar cal2 = new Calendar(cal.toString());
		assertEquals("Calendar toString(), from String",0,
				cal.toString().compareTo(cal2.toString()));
		
		/*save and load functionality*/
		cal.saveCalendar();
		cal2 = Calendar.loadCalendar();
		assertEquals("Calendar toString(), from String",0,
				cal.toString().compareTo(cal2.toString()));
	}
	@Test
	public void testMedicalHistory(){
		MedicalHistory a = new MedicalHistory("test.txt");
		a.setMedicalHistory("wooooooo history!");
		assertEquals("testing read/write","wooooooo history!\n",a.getMedicalHistory());
	}
	@Test
	public void testTreatmentRecord(){
		TreatmentRecord tr = new TreatmentRecord("tr");
		assertEquals("Treatment record to/from string", tr.toString(),
				TreatmentRecord.fromString(tr.toString()).toString());
	}
	@Test
	public void testBillingInformation(){
		BillingInformation bi = new BillingInformation("Name","Address","creditcard");
		assertEquals("Billing Information to/from string", bi.toString(),
				TreatmentRecord.fromString(bi.toString()).toString());
	}
	@Test
	public void testDoctorsOrders(){
		DoctorsOrders docOrd = new DoctorsOrders("Name","Address","creditcard","whut");
		assertEquals("Doctors Orders to/from string", docOrd.toString(),
				TreatmentRecord.fromString(docOrd.toString()).toString());
		
	}
	@Test
	public void testUserPersistence(){
		TreatmentRecord tr = new TreatmentRecord("tr");
		BillingInformation bi = new BillingInformation("Name","Address","creditcard");
		DoctorsOrders docOrd = new DoctorsOrders("dvdf","gdfgv","crefgd","whut");
		MedicalHistory mh = new MedicalHistory("test.txt");
		BasicInfo baseInf = new BasicInfo("sad", "thingy", "test", "case", "yeah");
		User user = new User("name", "name2", "string", "string 2", 
				"string 3", "other String", "last String", baseInf, mh, docOrd, tr, bi);
		assertEquals("User to/from save string", user.toSaveString(), 
				User.fromString(user.toSaveString()).toSaveString());
	}
	@Test
	public void testDatabaseUsers(){
		Runtime.getRuntime().addShutdownHook(new Thread() {
				public void run() {
					DatabaseUsers.saveUsers();
				}
		});
		
		DatabaseUsers test = new DatabaseUsers();
		User[] users = DatabaseUsers.users;
		
		
		/*At which point the user file can be checked for completeness*/
	}
	@Test
	public void testCrypto(){
		String s="";
		for (char i=0; i<128; i++){
			s+=i;
			assertEquals("Sequential String Encrypt/Decrypt",
					s,
					Cryptography.decrypt(Cryptography.encrypt(s)));
		}
		for (char i=160; i<256; i++){
			s+=i;
			assertEquals("Sequential String Encrypt/Decrypt",
					s,
					Cryptography.decryptByte(Cryptography.encryptByte(s)));
		}
		
	}
	@Ignore("Bad ascii values")
	@Test
	public void testCryptoPadding(){
		String s="";
		for (char i=128; i<160; i++){
			s+=i;
			assertEquals("Sequential String Encrypt/Decrypt",
					s,
					Cryptography.decrypt(Cryptography.encrypt(s)));
		}	
	}
}
